package cn.edu.tju.elm.service;

import cn.edu.tju.core.model.User;
import cn.edu.tju.core.security.service.UserService;
import cn.edu.tju.elm.model.Business;
import cn.edu.tju.elm.model.Remark;
import cn.edu.tju.elm.repository.BusinessRepository;
import cn.edu.tju.elm.repository.RemarkRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;

@Service
public class RemarkService {

    @Autowired
    private RemarkRepository remarkRepository;

    @Autowired
    private UserService userService;

    @Autowired
    private BusinessRepository businessRepository;

    /**
     * 根据商家ID获取所有评价
     * @param businessId 商家ID
     * @return 评价列表
     */
    public List<Remark> listRemarksByBusinessId(Long businessId) {
        return remarkRepository.findByBusinessId(businessId);
    }

    /**
     * 保存评价
     * @param remark 评价对象
     * @return 保存后的评价
     * @throws IllegalArgumentException 参数非法
     */
    public Remark saveRemark(Remark remark) {
        if (remark.getUser() == null || remark.getUser().getId() == null) {
            throw new IllegalArgumentException("用户不能为空");
        }

        if (remark.getBusiness() == null || remark.getBusiness().getId() == null) {
            throw new IllegalArgumentException("商家不能为空");
        }

        if (remark.getRemark() == null || remark.getRemark().trim().isEmpty()) {
            throw new IllegalArgumentException("评价内容不能为空");
        }

        // 验证用户是否存在
        Optional<User> userOpt = userService.getUserById(remark.getUser().getId());
        if (userOpt.isPresent()) {
            remark.setUser(userOpt.get());
        } else {
            throw new IllegalArgumentException("指定的用户不存在");
        }

        // 验证商家是否存在
        Optional<Business> businessOpt = businessRepository.findById(remark.getBusiness().getId());
        if (businessOpt.isPresent()) {
            remark.setBusiness(businessOpt.get());
        } else {
            throw new IllegalArgumentException("指定的商家不存在");
        }

        // 设置评价日期
        remark.setRemarkDate(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        return remarkRepository.save(remark);
    }

    /**
     * 删除用户的所有评价
     * @param userId 用户ID
     * @return 删除后的评价列表（应该为空）
     */
    @Transactional
    public List<Remark> removeAllRemarksByUserId(Long userId) {
        List<Remark> remarks = remarkRepository.findByUserId(userId);
        remarkRepository.deleteByUserId(userId);
        return remarks;
    }

    /**
     * 删除用户指定的评价
     * @param userId 用户ID
     * @param remarkId 评价ID
     * @return 删除后的评价列表
     */
    @Transactional
    public List<Remark> removeOneRemarkByUserId(Long userId, Long remarkId) {
        List<Remark> remarks = remarkRepository.findByUserId(userId);
        remarkRepository.deleteByUserIdAndId(userId, remarkId);
        return remarkRepository.findByUserId(userId);
    }

    /**
     * 根据用户ID获取所有评价
     * @param userId 用户ID
     * @return 评价列表
     */
    public List<Remark> getRemarksByUserId(Long userId) {
        return remarkRepository.findByUserId(userId);
    }

    /**
     * 根据用户ID和商家ID获取评价
     * @param userId 用户ID
     * @param businessId 商家ID
     * @return 评价列表
     */
    public List<Remark> getRemarksByUserIdAndBusinessId(Long userId, Long businessId) {
        return remarkRepository.findByUserIdAndBusinessId(userId, businessId);
    }
}

